package com.ipartek.desarrollo.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.ipartek.desarrollo.bean.MatriculasElearning;
import com.ipartek.desarrollo.hibernate.HibernateUtil;
import com.ipartek.desarrollo.interfaces.IMatriculasElearning;

public class MatriculasElearningDAO extends
		GenericDAOImpl<MatriculasElearning, Long> implements
		IMatriculasElearning {

	@Override
	public List<MatriculasElearning> ListarMatriculas() {
		List<MatriculasElearning> matriculasElearning = new ArrayList<MatriculasElearning>();
		Session session = HibernateUtil.getSession();
		SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
		Date fecha = null;
		try {
			fecha = formato.parse("2013-02-01");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		try {
			session.beginTransaction();
			Query q = session.createQuery("from MatriculasElearning m "
					+ "inner join fetch m.comprasPp co "
					+ "left join fetch co.partners p"
					+ "inner join fetch co.cursos cu "
					+ "inner join fetch cu.categorias "
					+ "where co.paymentDate >= :fecha "
					+ "order by co.paymentDate desc");
			q.setDate("fecha", fecha);
			matriculasElearning = q.list();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return matriculasElearning;
	}
}